#ifndef __TIMER_H__
#define __TIMER_H__
//***********************************************************************
// FILE: Timer.h
// AUTHOR: Yuriy Skriyka
// DATE: 27-03-2012 
// DESCRIPTION: The file contains implementation of the basic timer. 
//************************************************************************


#include <inttypes.h>
#include <avr/io.h>

// use Pulse Width Modulation to control led power.
#define TIMER_1MS_TICKS    150

#define USE_PWM

//void initTimer(void);

#ifdef USE_PWM
   #define initTimer()        \
         {                    \
         /*   OCR0B = 0xFF;            0xFF to insure */      \
            TCCR0A = _BV(WGM01) | _BV(WGM00) | _BV(COM0B1); /*clear timer on OCR0A compare match; Clear OC0B on compare match*/      \
            TCCR0B = _BV(WGM02) | _BV(CS01) | _BV(CS00);   /* F/64 prescaler */                        \
            OCR0A = TIMER_1MS_TICKS;                      /*// each 150 tick of the timer equals 1ms interval*/ \
            TIMSK0 = _BV(OCIE0A);    /* enable compare interrupt for OCR0A */     \
         }
   
   #define SET_LED_PWM(x)   OCR0B = (x); TCCR0A |= _BV(COM0B1);
   #define IS_LED_ON()      (TCCR0A & _BV(COM0B1))
   #define TURN_PWM_OFF()   TCCR0A &= ~_BV(COM0B1);
#else
   #define initTimer()        \
         {                    \
            TCCR0A = _BV(WGM01);              /*clear timer on compare match mode*/      \
            TCCR0B = _BV(CS01) | _BV(CS00);   /* F/64 prescaler */                        \
            OCR0A = TIMER_1MS_TICKS;                      /*// each 150 tick of the timer equals 1ms interval*/ \
            TIMSK0 = _BV(OCIE0A);    /* enable compare interrupt for OCR0A */     \
         }
#endif  // USE_PWM


// The file returns the time in milliseconds from the system start
uint32_t getTimer(void);

// sleep
void timerSleep(uint32_t milliseconds);


#endif // __TIMER_H__